//
//  XDLMViewController.m
//  xindailianmeng
//
//  Created by 许佳奇 on 17/1/17.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDLMViewController.h"
#import "XDLMNavigatonController.h"
#import "XDLMHomeViewController.h"
#import "XDLMInvestViewController.h"
#import "XDLMDiscoveryViewController.h"
#import "XDLMMyViewController.h"
#import "XDLMLoginViewController.h"
#import "XDLMRootViewController.h"
#import "AppDelegate.h"
#import "XDLMRegisteViewController.h"
// 我的投资  测试
#import "XDMyInviteViewController.h"

@interface XDLMViewController ()<UITabBarControllerDelegate>

@end

@implementation XDLMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}

- (void)createUI {
    NSArray * classArray = @[
                             
        @"XDLMHomeViewController",
        @"XDLMInvestViewController",
        @"XDLMDiscoveryViewController",
        @"XDLMMyViewController"
        
        ];
    NSArray * imageArray = @[@"home",@"invest",@"discovery",@"my"];
    NSArray * icoImageArray = @[@"home2",@"invest2",@"find",@"my2"];
    NSArray * titleArray = @[@"首页", @"投资", @"发现", @"我的"];
    NSMutableArray * vcs = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageArray.count; i++) {
        
        UIViewController * vc = [[NSClassFromString(classArray[i]) alloc] init];
        
        UIImage * image = [[UIImage imageNamed:imageArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage * icoImage = [[UIImage imageNamed:icoImageArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[i] image:image selectedImage:icoImage];
        
        vc.tabBarItem.tag = i;
        XDLMNavigatonController * nav = [[XDLMNavigatonController alloc] initWithRootViewController:vc];
        [vcs addObject:nav];
        
    }
    [self.tabBar setClipsToBounds:YES];
    [self.tabBar setTintColor:[UIColor colorWithRed:255.0/255.0 green:124.0/255.0 blue:56.0/255.0 alpha:1]];
    self.tabBar.backgroundImage = [UIImage imageNamed:@"mark"];
    self.viewControllers = vcs;
    self.delegate = self;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
   
    if (viewController.tabBarItem.tag == 3) {
        NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
        if ([df objectForKey:@"uid"]) {
            
            return YES;
            
        } else {
            
            XDLMLoginViewController *loginVc = [[XDLMLoginViewController alloc] init];
            loginVc.hidesBottomBarWhenPushed = YES;
            loginVc.isTip = YES;
            
            [df setValue:@"Y" forKey:@"isMine"];
            [df synchronize];
            XDLMNavigatonController * nav = [[XDLMNavigatonController alloc] initWithRootViewController:loginVc];
            AppDelegate *delegate =(AppDelegate *) [UIApplication sharedApplication].delegate;
            [delegate.window.rootViewController presentViewController:nav animated:YES completion:nil];
            return NO;
        }
    }
    return YES;
}

@end
